//
//  UserDao.m
//  SportSocial
//
//  Created by yull on 15/1/20.
//  Copyright (c) 2015年 yull. All rights reserved.
//

#import "UserDao.h"

@implementation UserDao

+ (instancetype)shareInstance
{
    static UserDao *userDao = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        userDao = [[UserDao alloc] init];
    });
    return userDao;
}

- (void)createTable
{
    [[DBManager shareInstance].dbQueue inDatabase:^(FMDatabase *db) {
        NSMutableArray *sqlArray = [NSMutableArray arrayWithObjects:
                             @"CREATE TABLE IF NOT EXISTS user ("
                             "[id] integer NOT NULL PRIMARY KEY AUTOINCREMENT, "
                             "[userId] integer,"
                             "[name] varchar,"
                             "[icon] varchar,"
                             "[sex] integer)"
                             ,nil];
        
        [sqlArray addObject:@"CREATE UNIQUE INDEX IF NOT EXISTS [idx_user_userId] ON [user] ([userId]);"];
        
        [db beginTransaction];
        BOOL result = YES;
        for (NSString *sql in sqlArray) {
            result &= [db executeUpdate:sql];
        }
        if (result) {
            result &= [db commit];
        }
        
        if (!result) {
            [db rollback];
        }
    }];
}

- (BOOL)addOrUpdateUser:(UserModel *)user
{
    BOOL isExist = [self checkIsExist:user];
    if (isExist) {
        return [self updateUser:user];
    }else {
        return [self insertUser:user];
    }
}

- (BOOL)checkIsExist:(UserModel *)user
{
    __block BOOL result = NO;
    
    [[DBManager shareInstance].dbQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *resultSet = [db executeQuery:@"select * from user where userId=?",[NSNumber numberWithLongLong:user.userId]];
        if (resultSet.next) {
            result = YES;
        }
        [resultSet close];
    }];
    return result;
}

- (BOOL)updateUser:(UserModel *)user
{
    __block BOOL result = YES;
    
    [[DBManager shareInstance].dbQueue inDatabase:^(FMDatabase *db) {
        result = [db executeUpdate:@"update user set name=?,icon=?,sex=? where userId=?",
                  user.name,
                  user.icon,
                  [NSNumber numberWithInteger:user.sex],
                  [NSNumber numberWithLongLong:user.userId]];
    }];
    return result;
}

- (BOOL)insertUser:(UserModel *)user
{
    __block BOOL result = YES;
    
    [[DBManager shareInstance].dbQueue inDatabase:^(FMDatabase *db) {
        result = [db executeUpdate:@"insert or replace into user (userId,name,icon,sex) values(?,?,?,?)",
                  [NSNumber numberWithLongLong:user.userId],
                  user.name,
                  user.icon,
                  [NSNumber numberWithInteger:user.sex]];
    }];
    return result;
}

- (UserModel *)selectUserWithId:(UInt64)userId
{
    __block UserModel *user = nil;
    
    [[DBManager shareInstance].dbQueue inDatabase:^(FMDatabase *db) {
        FMResultSet *resultSet = [db executeQuery:@"select * from user where userId=?",[NSNumber numberWithLongLong:userId]];
        if (resultSet.next) {
            user = [UserModel userWithResultSet:resultSet];
        }
        [resultSet close];
    }];
    return user;
}
@end
